# Maintainer: Hurcan Solter <hsolter@gmail.com>

_realname=armadillo
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=12.2.0
pkgrel=1
pkgdesc="Fast C++ matrix library with syntax similar to MATLAB and Octave (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://arma.sourceforge.io/"
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-superlu"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-arpack")
         )
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
install=${_realname}-${MSYSTEM}.install
source=(https://sourceforge.net/projects/arma/files/${_realname}-${pkgver}.tar.xz
        0001-mingw-config-fix.patch
        0002-fix-pkgconfig-file.patch)
sha256sums=('b0dce042297e865add3351dad77f78c2c7638d6632f58357b015e50edcbd2186'
            '772719e60eb2970ecb37844382dbcb6d0439f949c5080f9865798115640b612a'
            '830adb017e12c0e90671471dc6870a28ee21755a55e95280a04f73c2d65d665b')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  apply_patch_with_msg \
    0001-mingw-config-fix.patch \
    0002-fix-pkgconfig-file.patch
}

build() {
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}"/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_BUILD_TYPE=Release \
    -DOPENBLAS_PROVIDES_LAPACK=ON \
    ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  # Prepend build directory with PATH to get armadillo DLL file
  PATH=$PWD:$PATH "${MINGW_PREFIX}"/bin/ctest.exe .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR=${pkgdir} "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -d "${pkgdir}${MINGW_PREFIX}"/share/doc/${_realname}
  install -m644 "${srcdir}/${_realname}-${pkgver}/"*{.html,.png,.pdf,README.md} \
    "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.txt" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt"

  # Remove builders local installation path
  local _PREFIX_WIN=$(cygpath -m ${MINGW_PREFIX})
  sed -s "s|${_PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}/include/armadillo_bits/config.hpp"
  sed -e "s|${_PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i "${pkgdir}${MINGW_PREFIX}/share/Armadillo/CMake/ArmadilloLibraryDepends.cmake"
}
